import React from 'react';
import { message, TreeSelect } from 'antd';
import { history } from 'umi'

const { TreeNode } = TreeSelect;

// 格式化结果, 因为这个页面的行政机构只能有一个值, 但是服务端的返回的是一个数组, 要预处理.
export const formatResult = res => {
    if (res) {
        const { code, msg } = res;
        if (code === 10000) {
            const result = res.data;
            result.administrativeScope = result.administrativeScope?.length ? result.administrativeScope[0] : '';
            return result;
        }
        // 配置已变更, 当前访问的数据是旧数据, 要返回列表并刷新列表
        if (code === 21008) {
            message.warning(msg);
            history.push({
                pathname: '/office/personnelManagement/onboardAndLeave/setting/index',
                state: { tabRoute: true, tabRefresh: true, closeTab: true }
            });
            return undefined;
        }
        message.error(msg);
    }
    return undefined;
};

// 处理人所在的岗位必须是在该行政机构下面的. 通过自定义的方式来优化禁用的样式
export const generateTreeNodes = treeData =>
    treeData.map(item => (
        <TreeNode
            value={item.value}
            key={item.key}
            title={item.title}
            selectable={item.selectable}
            className={!item.selectable ? 'disabled' : null}
        >
            {item.children?.length && generateTreeNodes(item.children)}
        </TreeNode>
    ));

export const getItem = value => {
    let val = value;
    if (Array.isArray(value)) {
        val = value.join('、')
    }
    return (val !== undefined && val !== null ? val : '--')
};

export const getShowName = (target, origin) => {
    const dataList = target || [];
    const nameList = origin.map(item => (dataList.includes(item.id) ? item.detailLabel || item.label : '')).filter(v => !!v);
    return nameList.join('、');
};
